﻿#include "cd_list.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void* _cd_list_create_head() {
	CARDAN_LIST_NODE* ph = (CARDAN_LIST_NODE*)malloc(sizeof(CARDAN_LIST_NODE));
	ph->next = ph;
	ph->pre = ph;
	return ph;
}

void _cd_list_add_item(CARDAN_LIST_NODE* ph, CARDAN_LIST_NODE* it) {
	CARDAN_LIST_NODE* ptail = ph->pre;
	ptail->next = it;
	it->next = ph;
	it->pre = ptail;
	ph->pre = it;
}

int _cd_list_delete_item(CARDAN_LIST_NODE* ph, CARDAN_LIST_NODE* it) {
    if(ph == it) return -1;

    it->next->pre = it->pre;
    it->pre->next = it->next;
    free(it);
    return 0;
}

void _cd_list_release(CARDAN_LIST_NODE* ph) {
	CARDAN_LIST_NODE* pIt = ph->next;

	while(ph != pIt) {
		CARDAN_LIST_NODE* pTmp = pIt->next;
		free(pIt);
		pIt = pTmp;
	}
	free(ph);
}